//
//  ScrollPageView.m
//  testPageViewController
//
//  Created by tan on 16/11/23.
//  Copyright © 2016年 tan. All rights reserved.
//

#import "ScrollPageView.h"


#define ImageViewTagStart 100

@interface ScrollPageView ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIImageView *imageView1;
@property (nonatomic, strong) UIImageView *imageView2;
@property (nonatomic, strong) UIImageView *imageView3;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageCtr;

@end


@implementation ScrollPageView

- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"%s ", __PRETTY_FUNCTION__);
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"%s ", __PRETTY_FUNCTION__);
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self layout];
}

- (void)initUI
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.contentSize = self.frame.size;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
    
    self.pageCtr = [[UIPageControl alloc] initWithFrame:CGRectZero];
    self.pageCtr.backgroundColor = [UIColor clearColor];
    self.pageCtr.currentPageIndicatorTintColor = [UIColor brownColor];
    self.pageCtr.pageIndicatorTintColor = [UIColor grayColor];
    self.pageCtr.currentPage = 0;
    [self.pageCtr addTarget:self action:@selector(onPageControllerValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.pageCtr];
    
    self.imageView1 = [[UIImageView alloc] init];
    self.imageView2 = [[UIImageView alloc] init];
    self.imageView3 = [[UIImageView alloc] init];
    
    [self.scrollView addSubview:self.imageView1];
    [self.scrollView addSubview:self.imageView2];
    [self.scrollView addSubview:self.imageView3];
}

- (void)layout
{
    self.scrollView.frame = self.bounds;
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.frame) * 3, CGRectGetHeight(self.frame));
    self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.frame), 0);
    self.pageCtr.frame = CGRectMake((CGRectGetWidth(self.frame) - 100) / 2, CGRectGetHeight(self.frame) - 40, 100, 30);
    
    self.imageView1.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.imageView2.frame = CGRectMake(CGRectGetWidth(self.frame), 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    self.imageView3.frame = CGRectMake(CGRectGetWidth(self.frame) * 2, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
}

- (void)setImageURLs:(NSArray *)imageURLs
{
    _imageURLs = imageURLs;
    
    self.pageCtr.numberOfPages = imageURLs.count;
    self.pageCtr.currentPage = 0;
    
    [self updateImages];
    [self adjustContentOffset];
}

- (void)adjustContentOffset
{
    self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.frame), 0);
}

- (void)updateImages
{
    NSInteger index = self.pageCtr.currentPage - 1;
    if (index < 0) {
        index = self.pageCtr.numberOfPages - 1;
    }
    self.imageView1.image = [UIImage imageWithContentsOfFile:self.imageURLs[index].path];
    
    index = self.pageCtr.currentPage;
    self.imageView2.image = [UIImage imageWithContentsOfFile:self.imageURLs[index].path];
    
    index = self.pageCtr.currentPage + 1;
    if (index >= self.pageCtr.numberOfPages) {
        index = 0;
    }
    self.imageView3.image = [UIImage imageWithContentsOfFile:self.imageURLs[index].path];
}

- (void)onPageControllerValueChanged:(id)sender
{
    NSLog(@"%s ", __PRETTY_FUNCTION__);
//    [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.frame) * self.pageCtr.currentPage, 0) animated:YES];
    [self updateImages];
    [self adjustContentOffset];
}

//减速停止时触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    int offset = (int)round(scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame)) - 1;
    NSInteger index = self.pageCtr.currentPage + offset;
    if (index < 0) {
        index = self.pageCtr.numberOfPages - 1;
    }
    if (index >= self.pageCtr.numberOfPages) {
        index = 0;
    }
    
    self.pageCtr.currentPage = index;
    
    [self updateImages];
    [self adjustContentOffset];
}

- (void)addTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(onTimeout:) userInfo:nil repeats:YES];
}

- (void)removeTimer {
    if (_timer) {
        if ([self.timer isValid]) {
            [self.timer invalidate];
            _timer = nil;
        }
    }
}

//开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    //关闭定时器(注意,定时器一旦被关闭,无法在开启)
    [self removeTimer];
}

//拖拽结束后
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    //开启定时器
    [self addTimer];
}

//定时器执行方法
- (void)onTimeout:(NSTimer *)timer {
    if (self.pageCtr.currentPage == self.pageCtr.numberOfPages - 1) {
        self.pageCtr.currentPage = 0;
    } else if (self.pageCtr.currentPage < self.pageCtr.numberOfPages - 1) {
        self.pageCtr.currentPage++;
    }
    
    [self updateImages];
    [self adjustContentOffset];
}

@end
